package com.bwie.common.handler;

import com.bwie.common.exceptions.BizExceptions;
import com.bwie.common.result.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;

/**
 * @className: Handler
 * @Description: TODO
 * @version: v1.８.0
 * @author: 汪菲阳
 * @date: 2025/8/24 9:33
 */
@RestControllerAdvice
@Log4j2
public class Handler extends RuntimeException{
    @ExceptionHandler(BizExceptions.class)
    public Result bizExceptions(BizExceptions bizExceptions){
        log.error("业务异常信息处理：{}",bizExceptions.getMassage());
        return Result.error(bizExceptions.getMassage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result methodArgumentNotValidException(MethodArgumentNotValidException methodArgumentNotValidException){
        List<ObjectError> allErrors = methodArgumentNotValidException.getAllErrors();
        StringBuffer stringBuffer = new StringBuffer();
        for (ObjectError allError : allErrors) {
            String defaultMessage = allError.getDefaultMessage();
            stringBuffer.append(defaultMessage);
        }
        log.error("参数异常消息处理：{}",methodArgumentNotValidException.getMessage());
        return Result.error(stringBuffer.toString());
    }
}
